﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snake
{
    /// <summary>
    /// Snake类
    /// </summary>
    class Snake
    {
        public IGameComponent Head { get; set; }//蛇头
        public List<IGameComponent> Components { get; set; }//蛇身
        public SnakeDirection Direction { get; set; }
        public Graphics Graphics { get; set; }
        private List<Point> freepoints;

        public Snake(List<Point> freepoints)
        {
            Components = new List<IGameComponent>();
            this.freepoints = freepoints;
        }

        public void Refresh(Refreshflag refreshflag)//此处实现蛇运动及增加方块逻辑
        {
            if (Direction == SnakeDirection.None) return;
            var tail_position = Components.Last().Position;
            Components.Last().Color = new SolidBrush(GameComponentFactory.ColorOfBackground);
            Components.Last().Show(Graphics);
            for(int i = Components.Count - 1; i >= 1; i--)
            {
                Components[i].Position = Components[i - 1].Position;
            }
            Components.First().Position = Head.Position;
            switch (Direction)
            {
                case SnakeDirection.Up:
                    Head.Position = new Point(Head.Position.X, Head.Position.Y - 1);
                    break;
                case SnakeDirection.Down:
                    Head.Position = new Point(Head.Position.X, Head.Position.Y + 1);
                    break;
                case SnakeDirection.Left:
                    Head.Position = new Point(Head.Position.X - 1, Head.Position.Y);
                    break;
                case SnakeDirection.Right:
                    Head.Position = new Point(Head.Position.X + 1, Head.Position.Y);
                    break;
            }
            switch(refreshflag)
            {
                case Refreshflag.None:
                    freepoints.Add(tail_position);
                    break;
                case Refreshflag.EatFood:
                    var newbody = GameComponentFactory.CreateSnakeBody(tail_position);
                    Components.Add(newbody);
                    newbody.Show(Graphics);
                    break;
                case Refreshflag.TouchTrap:
                    freepoints.Add(Components.Last().Position);
                    Components.Last<IGameComponent>().Color = new SolidBrush(GameComponentFactory.ColorOfBackground);
                    Components.Last().Show(Graphics);
                    Components.Remove(Components.Last());
                    freepoints.Add(tail_position);
                    break;
            }
            Components.First().Show(Graphics);
            freepoints.Remove(Head.Position);
        }

        public void Init(List<IGameComponent> components)
        {
            throw new NotImplementedException();
        }

        public enum SnakeDirection
        {
            Left,
            Right,
            Up,
            Down,
            None
        }

        public enum Refreshflag
        {
            None,
            EatFood,
            TouchTrap,
            //DrinkPotion,
        }
    }
}
